﻿namespace MultipleMonitorVideo.ViewModels
{
    using System;
    using System.Windows.Input;
    using Microsoft.Win32;

    public class OverlayInfoControlViewModel : ModuleControlViewModel
    {
        private OverlayInfoViewModel overlayInfoViewModel;
        private string fileName;
        private string overlayText;
        
        public OverlayInfoControlViewModel()
        {
        }

        public string FileName
        {
            get
            {
                return this.fileName;
            }

            set
            {
                if (this.fileName != value)
                {
                    this.fileName = value;
                    this.NotifyOfPropertyChange();
                }
            }
        }

        public string OverlayText
        {
            get
            {
                return this.overlayText;
            }

            set
            {
                if (this.overlayText != value)
                {
                    this.overlayText = value;
                    this.NotifyOfPropertyChange();
                }
            }
        }

        public override void SetViewModel(ViewModel viewModel)
        {
            this.SetViewModel(viewModel as OverlayInfoViewModel);
        }

        public void SetViewModel(OverlayInfoViewModel viewModel)
        {
            this.overlayInfoViewModel = viewModel;
        }

        public void SelectFile()
        {
            var openFileDialog = new OpenFileDialog
            {
                Filter = "Video Files|*.avi;*.flv;*.mkv;*.mov;*.mpeg;*.mpg;*.mp4;*.ogg;*.webm;*.wmv|All Files|*.*"
            };

            bool? userClickedOk = openFileDialog.ShowDialog();

            if (userClickedOk == true)
            {
                string fileName = openFileDialog.FileName;
                this.FileName = fileName;
            }
        }

        public void PlayNewVideo()
        {
            if (this.overlayInfoViewModel != null)
            {
                if (string.IsNullOrEmpty(this.FileName))
                {
                    return;
                }
                
                this.overlayInfoViewModel.FileName = new Uri(this.FileName);
                this.overlayInfoViewModel.PlayVideo();
            }
        }

        public void UpdateOverlayText()
        {
            if (this.overlayInfoViewModel != null)
            {
                this.overlayInfoViewModel.OverlayText = this.overlayText;
            }
        }
    }
}
